import * as THREE from 'three'
// 公转轨迹圆弧线
export const circle = (r:number) => {
  const arc = new THREE.ArcCurve(0, 0, r, 0, 2 * Math.PI, true) // 圆心  半径  起始角度
  const points = arc.getPoints(50) // 返回一个vector2对象作为元素组成的数组
  const geometry = new THREE.BufferGeometry()
  geometry.setFromPoints(points)
  const material = new THREE.LineBasicMaterial({
    color: 0x222222
  })
  const line = new THREE.LineLoop(geometry, material)
  line.rotateX(Math.PI / 2)
  return line
}
export const createSun = (radius:number) => {
  // 球体
  const geom = new THREE.SphereGeometry(radius, 32, 16)
  // 纹理
  const loader = new THREE.TextureLoader()
  const texture = loader.load('https://solar-system-1305245473.cos.ap-shanghai.myqcloud.com/solar-system/sun.jpg')
  // 材质
  const material = new THREE.MeshBasicMaterial({
    map: texture
  })
  const earth = new THREE.Mesh(geom, material)
  return earth
}
export const createGeom = (radius:number, photo:string) => {
  // 球体
  const geom = new THREE.SphereGeometry(radius, 32, 16)
  // 纹理
  const loader = new THREE.TextureLoader()
  const texture = loader.load(`https://solar-system-1305245473.cos.ap-shanghai.myqcloud.com/solar-system/${photo}.jpg`)
  // 材质
  const material = new THREE.MeshLambertMaterial({
    map: texture
  })
  const earth = new THREE.Mesh(geom, material)
  return earth
}
const createRingGeom = (r:number, R:number, photo:string) => {
  const geom = new THREE.CylinderGeometry(r, R, 0, 100, 100, true)
  const loader = new THREE.TextureLoader()
  const texture = loader.load(`https://solar-system-1305245473.cos.ap-shanghai.myqcloud.com/solar-system/${photo}.jpg`)
  // 材质
  const material = new THREE.MeshBasicMaterial({
    map: texture
  })
  const ring = new THREE.Mesh(geom, material)
  return ring
}
export const createRingPlant = (radius:number, photo:string, r:number, R:number, ringPhoto:string) => {
  const Group = new THREE.Group()
  const Planet:any = createGeom(radius, photo)
  const ring:any = createRingGeom(r, R, ringPhoto)
  Group.add(Planet, ring)
  return Group
}
// 星云
export const cloudFun = () => {
  const vertices = []
  for (let i = 0; i < 10000; i++) {
    const x = THREE.MathUtils.randFloatSpread(2000)
    const y = THREE.MathUtils.randFloatSpread(2000)
    const z = THREE.MathUtils.randFloatSpread(2000)
    vertices.push(x, y, z)
  }
  const geometry = new THREE.BufferGeometry()
  geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3))

  const material = new THREE.PointsMaterial({ color: 0x888888 })

  const points = new THREE.Points(geometry, material)
  return { geometry, points }
}
